/**
 * @author zhengzhaolong
 * @version 2017-05-04
 */

var employeeManageParams = {
	cache: {}, // 列表缓存数据
	params: {
		currentPage: 1,
		pageSize: 20,
		roleId: '', //角色id
		onjobStatus: '',//在职状态
		shopName: '',
		shopId: ''//门店Id
	}, // 查询参数
	isSucFlag:false,
	"handleBack4StoreManage" : ""
};

(function(doc, win) {
	var BindEvent = function(){
		/*//绑定下拉刷新
		refreshItem({
			"target": "#userManagePage .page-body",
			"callBack": function(reset) {
				employeeManageParams.params.currentPage = 1;
				var controller = new EmployeeController();
				controller.getEmployeeList(employeeManageParams.params, reset);
			}
		});*/

		addMoreItem({
			"target": "#userManagePage .page-body",
			"callBack": function(reset) {
				employeeManageParams.params.currentPage++;
				var controller = new EmployeeController();
				controller.getEmployeeList(employeeManageParams.params, reset);
			}
		});
	};

	if($('#userManagePage').length){
		BindEvent();
	}

	$(doc).on("pageAnimationStart", "#userManagePage", function(){
		BindEvent();
	});


	// sui 页面就绪时间
	$(doc).on("pageInit", "#userManagePage", function(e, pageId, $page, isBack) {
		
		if (isBack) {
			if (employeeFilterParam.params && employeeFilterParam.params.onjobStatus === employeeManageParams.params.onjobStatus &&
				employeeFilterParam.params.roleId === employeeManageParams.params.roleId &&
				employeeFilterParam.params.shopId === employeeManageParams.params.shopId) {
				return; // 搜索条件没变，不需要处理
			} else {
				employeeManageParams.params = {
					currentPage: 1,
					pageSize: 20,
					onjobStatus: employeeFilterParam.params.onjobStatus,
					roleId: employeeFilterParam.params.roleId,
					shopName: employeeFilterParam.params.shopName,
					shopId: employeeFilterParam.params.shopId
				};
				var controller = new EmployeeController();
				controller.getEmployeeList(employeeManageParams.params);
				
			}

			//设置选择icon
			setFilterIcon(orderFilterParam.singleShop);
		} else {
			employeeManageParams.params = {
				currentPage: 1,
				pageSize: 20,
				roleId: '', //角色
				onjobStatus: '',//在职状态
				shopName: '',
				shopId: ''//门店Id
			};
			employeeFilterParam.params = {
				onjobStatus: "",
				roleId: "",
				shopName: "",
				shopId: ""
			};
			if(sessionStorageClient.getJSON("user").role==4){
			employeeManageParams.params.shopId = sessionStorageClient.getJSON("user").selfShopId;
			employeeFilterParam.params.shopId = sessionStorageClient.getJSON("user").selfShopId;
			}
			if(employeeManageParams.isSucFlag) {
				employeeStoreListFilterParam.shopId = '';
				employeeStoreListFilterParam.shopName = '';
				$('#employeeFilterPage .filter-item').removeClass("selected");
				$('#employeeFilterPage  .page-body .store-name').html("全部门店");
				$('#employeeStoreListPage .scroll-view .check-all').css("color", "#508cee");
				$('#employeeStoreListPage .page-body #selectEmployeeShopDiv .store-list li').removeClass("selected");
			}
			//debugger
		   // var user1 = sessionStorageClient.getJSON("user");
		   // CommonUtil.render($("#userManagePage .page-body #userManageCotainer" ), "userManagerTPL", { role: user1.role });
			var controller = new EmployeeController();
			controller.getEmployeeList(employeeManageParams.params);

			//设置选择icon
			setFilterIcon(true);
		}
		
	});


	//添加员工页面
	$(doc).on("click", "#userManagePage a[name='add_user']", function () {
		$.router.load("employee_add.html");
	});
	$(document).on("click","#userManagePage .back",function(){
		var handleBack4StoreManage = employeeManageParams.handleBack4StoreManage;

		//清除handleBack
		var cache = CommonUtil.takeOutParamObject() || {};
		cache.handleBack4StoreManage = "";
		CommonUtil.injectParamObject(cache);
		employeeManageParams.handleBack4StoreManage = "";

		if(handleBack4StoreManage == "load"){
			$.router.load("my.html",true);
		}else{
			$.router.back();
		}
	});

	//筛选员工筛选
	$(doc).on("click", "#userManagePage .zhk-nav_filter", function () {
		CommonUtil.injectParamObject(employeeManageParams);
		$.router.load("employee_manage_filter.html");
	});
	//编辑员工页面
	$(doc).on("click", "#userManageCotainer li", function () {
		var roleId = $(this).attr("roleId");
		if(sessionStorageClient.getJSON("user").role==4 && roleId==4){
        DialogUtil.dialogWarn("您没有修改该员工权限!");
		 return;
		}
		var employeeId = $(this).attr("accountId");		
		CommonUtil.injectParamObject({
			employeeId: employeeId
		});
		$.router.load("employee_edit.html",true);
	});

	//设置筛选标签样式
	var setFilterIcon = function (singleShop) {
		if (employeeManageParams.params.roleId != ''
			|| employeeManageParams.params.onjobStatus != '') {
			$('#userManagePage .header .right .zhk-nav_filter').addClass("headIconUsed");
			return;
		}
		if (employeeManageParams.params.shopId != '' && !singleShop) {
			$('#userManagePage .header .right .zhk-nav_filter').addClass("headIconUsed");
			return;
		}
		$('#userManagePage .header .right .zhk-nav_filter').removeClass("headIconUsed");
	}
})(document, window);